home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / POINTERS.SWG / 0026_Sorting Linked Lists.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  1KB  |  64 lines

  1. {
  2. │ I'm looking for a routine to swap two nodes in a double
  3. │ linked list or a complete sort.
  4.  
  5. There has been a thread on the TP conf area in CIS on quick
  6. sorting a (double) linked list. To swap two nodes, remove one,
  7. then add it in where desired. Quick sample-
  8. }
  9.  
  10. type
  11.   s5       = string[5];
  12.   ntp      = ^nodetype;
  13.   nodetype = record
  14.                prv,nxt : ntp;
  15.                data    : s5;
  16.              end;
  17. const
  18.   nbr : array[0..9] of string[5] = ('ZERO','ONE','TWO',
  19.         'THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE');
  20. var
  21.   node,root : ntp;
  22.   i : integer;
  23.  
  24. procedure swap (var n1,n2 : ntp);
  25.   var n : ntp;
  26.   begin
  27.     n := n1;
  28.     n1 := n2;
  29.     n2 := n;
  30.   end;
  31.  
  32. procedure addnode (var n1,n2 : ntp);
  33.   begin
  34.     swap(n1^.nxt,n2^.prv^.nxt);
  35.     swap(n1^.prv,n2^.prv);
  36.   end;
  37.  
  38. procedure getnode(i:integer);
  39.   var n : ntp;
  40.   begin
  41.     getmem(n,sizeof(nodetype));
  42.     n^.nxt := n;
  43.     n^.prv := n;
  44.     n^.data := nbr[i];
  45.     if root=nil
  46.     then root := n
  47.     else addnode(n,root);
  48.   end;
  49.  
  50. begin
  51.   root := nil;
  52.   for i := 0 to 9 do
  53.   begin
  54.     getnode(i);
  55.     node := root;
  56.     writeln;
  57.     writeln('The linked now is-');
  58.     repeat
  59.       writeln(node^.data);
  60.       node := node^.nxt;
  61.     until node = root;
  62.   end;
  63. end.
  64.